/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "makecard_srv_proxy.h"
#include "ipc_types.h"
#include "message_option.h"
#include "message_parcel.h"
#include "hilog_wrapper.h"
#include "makecard_errors.h"
#include "makecard_common.h"
#include <string.h>

namespace SwanLink {
namespace Finance {

int MakecardSrvProxy::SetConfig(int handle, int configType, int configValue)
{
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::SetConfig(%{public}d,%{public}d,%{public}d)",
        handle, configType, configValue);
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) {
        return result;
    }
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MakecardSrvProxy::GetDescriptor())) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, configType, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, configValue, -1);

    int ret = remote->SendRequest(static_cast<int>(IMakecardSrv::MAKECARD_SETCONFIG), data, reply, option);
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::SetConfig(%{public}d,%{public}d,%{public}d) ret %{public}d",
        handle, configType, configValue, ret);
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);

    return result;
}

int MakecardSrvProxy::SetStandbyParam(int handle, int dir, int pos, int standbyTime)
{
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::SetStandbyParam(%{public}d,%{public}d,%{public}d,%{public}d)",
        handle, dir, pos, standbyTime);
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) {
        return result;
    }
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MakecardSrvProxy::GetDescriptor())) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, dir, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, pos, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, standbyTime, -1);

    int ret = remote->SendRequest(static_cast<int>(IMakecardSrv::MAKECARD_SETSTANDBYPARAM), data, reply, option);

    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::SetStandbyParam(%{public}d,%{public}d,%{public}d,%{public}d) out",
        handle, dir, pos, standbyTime);
    return result;
}

int MakecardSrvProxy::PrintCard(int handle, BmpInfo cardBmpInfo)
{
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::PrintCard(%{public}s,%{public}s,%{public}s,%{public}s) out",
        cardBmpInfo.lpFrontBGR, cardBmpInfo.lpFrontK, cardBmpInfo.lpBackBGR, cardBmpInfo.lpBackK);
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) {
        return result;
    }
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MakecardSrvProxy::GetDescriptor())) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, -1);
    MAKECARD_HILOGE(MAKECARD_SRV,
        "MakecardSrvProxy::PrintCard pre WRITE_BUFFER_WITH_RET(data, &cardBmpInfo, sizeof(BmpInfo), -1);");
    WRITE_BUFFER_WITH_RET(data, &cardBmpInfo, sizeof(BmpInfo), -1);
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::PrintCard pre sendrequest");
    int ret = remote->SendRequest(static_cast<int>(IMakecardSrv::MAKECARD_PRINTCARD), data, reply, option);
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::PrintCard  sendrequest");
    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);

    return result;
}

int MakecardSrvProxy::GetDeviceInfo(int handle, int infoType, char *infoValue)
{
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) {
        return result;
    }
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MakecardSrvProxy::GetDescriptor())) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, infoType, -1);

    int ret = remote->SendRequest(static_cast<int>(IMakecardSrv::MAKECARD_GETDEVICEINFO), data, reply, option);

    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    std::string strinfoValue;
    READ_PARCEL_WITH_RET(reply, String, strinfoValue, -1);
    strncpy(infoValue, strinfoValue.c_str(), strinfoValue.size());
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::GetDeviceInfo(%{public}d,%{public}s", infoType, infoValue);
    return result;
}

int MakecardSrvProxy::GetErrorString(int handle, int status, char* statusDesc)
{
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) {
        return result;
    }
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MakecardSrvProxy::GetDescriptor())) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, status, -1);

    int ret = remote->SendRequest(static_cast<int>(IMakecardSrv::MAKECARD_GETERRORSTRING), data, reply, option);

    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    std::string strstatusDesc;
    READ_PARCEL_WITH_RET(reply, String, strstatusDesc, -1);
    strcpy_s(statusDesc, strstatusDesc.size(), strstatusDesc.c_str());
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    return result;
}

int MakecardSrvProxy::MoveCard(int handle, int postionMoveTo)
{
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) {
        return result;
    }
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MakecardSrvProxy::GetDescriptor())) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, -1);
    WRITE_PARCEL_WITH_RET(data, Int32, postionMoveTo, -1);

    int ret = remote->SendRequest(static_cast<int>(IMakecardSrv::MAKECARD_MOVECARD), data, reply, option);

    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    MAKECARD_HILOGE(MAKECARD_SRV, "MakecardSrvProxy::MoveCard(%{public}d,%{public}d ret", handle, postionMoveTo);
    return result;
}

int MakecardSrvProxy::FlipCard(int handle)
{
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) {
        return result;
    }
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MakecardSrvProxy::GetDescriptor())) {
        MAKECARD_HILOGE(MAKECARD_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, -1);

    int ret = remote->SendRequest(static_cast<int>(IMakecardSrv::MAKECARD_FLIPCARD), data, reply, option);

    if (ret != ERR_OK) {
        MAKECARD_HILOGE(MAKECARD_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);

    return result;
}
}  // namespace Finance
}  // namespace SwanLink
